import request from "@/utils/request";

// 获取用户是否点赞帖子
export function getUserLikePost(data) {
	return request.get("/poster/like-states", {
		params: data,
	}).then(res => {
		const {
			s,
			d
		} = res
		let newData = {}
		if (d?.data && Array.isArray(d.data)) {
			d.data.forEach(item => {
				newData[item.poster_id] = item.state === 1
			})
		}
		return newData
	})
}

// 获取帖子列表
export async function getPost(data) {
	const res = await request.get("poster", {
		params: data,
	})
	const {
		d,
		s
	} = res || {}
	const _data = d?.data?.data;
	
	if (!_data || _data.length === 0) {
		return res
	}
	const ids = _data.map(item => item.id).join(',')
	const likedPosters = await getUserLikePost({
		ids
	})
	res.d.data.data = _data.map(item => {
		return {
			...item,
			isLiked: likedPosters[item.id]
		}
	})
	return res
}

// 获取账号信息
export function getAccountInfo(data) {
	return request.get("poster", {
		params: {
			userId: 74,
			page: 1,
			pagsize: 1
		},
	});
}

// 统计分享数量
export function updateShareNum(data) {
	return request.post(`poster/record-share-num/${data.id}`, data).then(res => {
		const {
			s,
			d
		} = res
		if (s === 0) {
			return d.user || {}
		}
	}).catch(e => {
		return {}
	});;
}

// 帖子点赞
export function likePost(data) {
	return request.post(`poster/like/${data.id}`, data).then(res => {
		const {
			s,
			d
		} = res
		return s === 0
	}).catch(e => {
		return false
	});;
}

// 评论点赞
export function likeCommentPost(data) {
	return request.post(`poster/comment/like/${data.id}`, data).then(res => {
		const {
			s,
			d
		} = res
		return s === 0
	}).catch(e => {
		return false
	});;
}

// 获取用户信息。
export function getUserInfo(data) {
	return request.get(`profile/${data.id}`, data).then(res => {
		const {
			s,
			d
		} = res
		if (s === 0) {
			return d.user || {}
		}
		return {}
	}).catch(e => {
		return {}
	});;
}

export function addComment(data) {
	const {
		id,
		content,
		father_id,
		reply_child_id,
		lat,
		lon,
	} = data
	return request.post(`poster/comment/${id}`, {
		content,
		reply_comment_id: reply_child_id || father_id || 0,
		lat,
		lon,
	}).then(res => {
		const {
			s,
			d
		} = res
		return d.comment
	}).catch(e => {
		return false
	});
}

// 获取用户是否点赞帖子
export function getUserCommentLikePost(data) {
	return request.get("poster/comment/like-states", {
		params: data,
	}).then(res => {
		const {
			s,
			d
		} = res
		let newData = {}
		if (d?.data && Array.isArray(d.data)) {
			d.data.forEach(item => {
				newData[item.comment_id] = item.state === 1
			})
		}
		return newData
	})
}

// 获取评论列表
export async function getComment(data) {
	const res = await request.get(`poster/comment/${data.id}`, {
		params: data,
	})
	const {
		d,
		s
	} = res || {}
	const _data = d?.data?.data
	if (!_data || _data.length === 0) {
		return []
	}
	const ids = _data.map(item => item.id).join(',')
	const likeComments = await getUserCommentLikePost({
		ids
	})
	return _data.map(item => {
		return {
			...item,
			isLiked: likeComments[item.id]
		}
	})

}